package org.ftc.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.map.ObjectMapper;
import org.ftc.dao.FtcLogDao;
import org.ftc.domain.FtcLog;
import org.ftc.form.FtcLogForm;
import org.ftc.util.ServlerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Service
public class FtcLogService {
	
	@Autowired
	FtcLogDao ftcLogDao;
	@Autowired
	UsersService usersService;
	@Autowired
	ServlerUtil servlerUtil;
	
	private Date startDateTime;
	
	public FtcLog load(String id) {
		// TODO Auto-generated method stub
		return ftcLogDao.load(id);
	}

	public FtcLog get(String id) {
		// TODO Auto-generated method stub
		return ftcLogDao.get(id);
	}
	
	public void before() {
		startDateTime = new Date();
	}
	
	public boolean save(Log action, String objectName ) {
		FtcLog entity = new FtcLog();
		entity.setAction(action.name());
		entity.setStartDateTime(startDateTime);
		entity.setObjectName(objectName);
		return this.save(entity); 
	}
	
	public boolean save(Log action, String objectID, String objectName ) {
		FtcLog entity = new FtcLog();
		entity.setAction(action.name());
		entity.setStartDateTime(startDateTime);
		entity.setObjectID(objectID);
		entity.setObjectName(objectName);
		return this.save(entity);
	}
	
	public boolean save(Log action, String objectID, String objectName ,String detail) {
		FtcLog entity = new FtcLog();
		entity.setAction(action.name());
		entity.setStartDateTime(startDateTime);
		entity.setObjectID(objectID);
		entity.setObjectName(objectName);
		entity.setDetail(detail);
		return this.save(entity);
	}
	
	public synchronized boolean save(FtcLog entity) {
		// TODO Auto-generated method stub
		Authentication userDetails = SecurityContextHolder.getContext().getAuthentication();
		entity.setApName("NSTC2014");
		entity.setEndDateTime(new Date());
		entity.setUrl(servlerUtil.getUrl());
		entity.setIpAddress(servlerUtil.getIP());
		entity.setUserName(userDetails != null ? userDetails.getName() : null );
		entity.setSession(servlerUtil.getRequest().getSession().getId());
		return ftcLogDao.save(entity); 
	}

	public synchronized boolean saveOrUpdate(FtcLog entity) {
		// TODO Auto-generated method stub
		return ftcLogDao.saveOrUpdate(entity);
	}

	public synchronized boolean update(FtcLog entity) {
		// TODO Auto-generated method stub
		return ftcLogDao.update(entity);
	}

	public synchronized boolean delete(FtcLog entity) {
		// TODO Auto-generated method stub
		return ftcLogDao.delete(entity);
	}

	public synchronized boolean delete(String id) {
		// TODO Auto-generated method stub
		return ftcLogDao.delete(id);
	}

	public List<FtcLog> findPage(int offset, int pageSize) {
		// TODO Auto-generated method stub
		return ftcLogDao.findPage(offset, pageSize);
	}

	public List<FtcLog> findAll() {
		// TODO Auto-generated method stub
		return ftcLogDao.findAll();
	}

	public long countAll() {
		// TODO Auto-generated method stub
		return ftcLogDao.countAll();
	}
	
	public List<FtcLog> findPage(FtcLogForm form ,int offset, int pageSize) {
		// TODO Auto-generated method stub
		return ftcLogDao.findPage(form, offset, pageSize);
	}

	public long count(FtcLogForm form) {
		// TODO Auto-generated method stub
		return ftcLogDao.count(form);
	}
	
	public List<FtcLog> findPage(FtcLogForm form ) {
		// TODO Auto-generated method stub
		return ftcLogDao.findPage(form);
	}
	
	@SuppressWarnings("unchecked")
	public List<HashMap<String,Object>> findPageMap(FtcLogForm form ) {
		// TODO Auto-generated method stub
		List<FtcLog> list = findPage(form);
		ObjectMapper m = new ObjectMapper();
		List<HashMap<String, Object>> listn = null;
		if( list != null ){
			listn = new ArrayList<HashMap<String, Object>>();
			for(FtcLog log : list ){
				listn.add( m.convertValue(log, HashMap.class) );
			}
		}
		return listn; 
	}
	
	public String ipAddress(){
		HttpServletRequest request = 
				((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
		if (request.getHeader("x-forwarded-for") == null) {
			return request.getRemoteAddr();
		}
	    return request.getHeader("x-forwarded-for");
	}
	
	public enum Log { 
	    INSERT("新增"),
	    UPDATE("修改"),
	    INSERT_UPDATE("新增/修改"),
	    DELETE("刪除"),
	    QUERY("查詢"),
	    EXP("匯出"),
	    DOWNLOAD("下載"),
	    SEND("發信"),
	    PASSWORD("密碼"),
	    FORGET("忘記密碼"),
	    LOGING("登入"),
	    LOGOUT("登出");
	    
	    private final String name;
	    Log(String name) {
	        this.name = name;
	    }
		public String getName() {
	        return name;
	    }
	}	
}
